// pages/addExpenseaccount/addExpenseaccount.js
var util = require('../../utils/util.js');
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time:'',
    array: ['请选择', '技术部', '行政部', '商务部'],
    index: 0,
    bmId: '',
    array2: ['请选择', '唐雪玲(财务)', '功力(总监)'],
    index2: 0,
    shrId: '',
    array3: ['请选择', '交通', '饮食'],
    index3: 0,
    // 费用金额
    csfylxJe: '',
    date1: '请选择',
    //  输入的备注
    textareaBz: '',
    //存文件名值
    fileName: '',
    // 存后台返回的文件名值
    filepostName:'',

    textkhmclist: '',
    khmcinput: '',
    khmcId: '',
    search: '',
    // 项目名称的
    array4: [{projectName:'无'}],
    index4: 0,

    textlxxmmc: '',
    xmmcId: '',
    // 存全部录入的值
    region: [],
    // 存传给后台的值
    regionpost: [],
    asdpost: {},

    loginName: app.globalData.loginName
  },
  bindPickerChange: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      bmId: this.data.array[e.detail.value].id
    })
  },
  bindPickerChange2: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value,
      shrId: this.data.array2[e.detail.value].id
    })
  },
  bindPickerChange3: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index3: e.detail.value
    })
  },
  bindPickerChange4: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index4: e.detail.value,
      textlxxmmc: this.data.array4[e.detail.value].projectStageName,
      xmmcId: this.data.array4[e.detail.value].Id
    })
  },
  // 获取费用金额输入的值
  getFyje: function (e) {
    console.log(e.detail);
    this.setData({
      csfylxJe: e.detail.value.replace(/\s+/g, '')
    })
  },
  // 获取备注输入的值
  getBz: function (e) {
    console.log(e.detail);
    this.setData({
      textareaBz: e.detail.value.replace(/\s+/g, '')
    })
  },
  bindDateChange1: function (e) {
    this.setData({
      date1: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      textxmmc: e.detail.value
    })
  },

  // 上传
  bindFilewenjian: function (e) {   
    var that = this; 
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        var fileBDsrc = res.tempFiles[0].path;
        var fileBDname = res.tempFiles[0].name;        
        wx.uploadFile({
          url: app.globalData.apiUrl+'/wx/wxscgn?filename='+fileBDname, //仅为示例，非真实的接口地址
          filePath: fileBDsrc,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success(res) {
            const data = res.data;       
            var jsondata = JSON.parse(data);     
            //do something
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 2000
            })
            that.setData({
              fileName:fileBDname,
              filepostName:jsondata.msg
            })
          },
          fail(e){
            wx.showToast({
              title: '网络繁忙请重新上传!',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  },
  // 客户名称模糊搜索
  changeSearch: function (event) {
    console.log(event.detail);
    let that = this;
    var inputSearch = event.detail.value;
    that.setData({
      search: inputSearch
    })
    that.getKhmc(inputSearch);
  },
  // 点击客户名称
  aKhmc: function (e) {
    console.log(e);

    if (e.target.dataset.id != "") {
      this.getXmmc(e.target.dataset.id);
    }
    this.setData({
      khmcinput: e.target.dataset.text,
      khmcId: e.target.dataset.id,
      // xmmcId: this.data.array4[0].Id,
      textlxxmmc: ''
    })
  },

  // 添加
  addPost: function (e) {
    var temp = this.data.region;
    var temppost = this.data.regionpost;
    var asde = this.data.asdpost;
    var lvbxbm = this.data.array[this.data.index].name;
    var lvbmid = this.data.bmId;
    var lvshr = this.data.array2[this.data.index2].username;
    var lvshrid = this.data.shrId;
    var lvfymc = this.data.array3[this.data.index3].name;
    var lvfyje = this.data.csfylxJe;
    var lvfssj = this.data.date1;
    var lvbz = this.data.textareaBz;
    var lvfilename = this.data.fileName;
    var lvfilepostname = this.data.filepostName;
    var lvkhmc = this.data.khmcinput;
    var lvkhmcid = this.data.khmcId;
    var lvxmmc = this.data.array4[this.data.index4].projectName;
    var lvxmmcid = this.data.xmmcId;
    var lvlxxmmc = this.data.textlxxmmc;
    if (lvxmmc == undefined) {lvxmmc = '';}
    if (lvlxxmmc == undefined) {lvlxxmmc = '';}
    if(lvfymc=='请选择'){wx.showToast({title: '请选择费用名称!',icon: 'none',duration: 2000});return}
    if(lvfyje==''){wx.showToast({title: '请输入费用金额!',icon: 'none',duration: 2000});return}
    if(lvfssj=='请选择'){wx.showToast({title: '请选择发生时间!',icon: 'none',duration: 2000});return}
    temp.push(lvbxbm + lvshr + lvfymc + lvfyje + lvfssj + lvbz + lvfilename + lvkhmc + lvxmmc + lvlxxmmc);
    asde = {
        expenseName: lvfymc,
        expenseAmount: lvfyje,
        createTime: lvfssj,
        remark: lvbz,
        detailurl: lvfilepostname,
        clientId: lvkhmcid,
        projectId: lvxmmcid
    };

    console.log(temp);
    console.log(temppost);
    console.log(asde);

    temppost.push(asde);
    //console.log(temppost);
    this.setData({
      region: temp,
      regionpost: temppost,
      fileName: '',
      filepostName: ''
      //asdpost:temppost
    })
  },
  // 删除
  delPost: function (e) {
    var temp = this.data.region;
    var temppost = this.data.regionpost;
    var asde = this.data.asdpost;
    temp.splice(e.currentTarget.id, 1);
    temppost.splice(e.currentTarget.id, 1);
    //asde.splice(e.currentTarget.id, 1);
    console.log(temp);
    console.log(temppost);
    console.log(asde);
    this.setData({
      region: temp,
      regionpost: temppost,
      //asdpost:asde
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var day = util.formatNewday(new Date());
    this.setData({
      time:day,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getDictByCode('reimburseUnit');
    this.getShr('15920881905');
    this.getFymc('reimbursementName');
    this.getKhmc('');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //加载报销部门数据字典
  getDictByCode: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/getDictByCode', //接口地址
      data: {
        code: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        that.setData({
          array: res.data.data,
          bmId: res.data.data[0].id
        })
      }
    })
  },
  //加载审核人数据字典
  getShr: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/addwxReimbursement', //接口地址
      data: {
        loginName: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        that.setData({
          array2: res.data.data.auditer,
          shrId: res.data.data.auditer[0].id
        })
      }
    })
  },
  //加载费用名称数据字典
  getFymc: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/getDictByCode', //接口地址
      data: {
        code: codes,
      },
      success: function (res) {
        res.data.data.unshift({name:'请选择'});
        console.log("数据字典=", res)
        that.setData({
          array3: res.data.data,
        })
      }
    })
  },
  //加载客户名称数据字典
  getKhmc: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxsousuo', //接口地址
      data: {
        customerName: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        that.setData({
          textkhmclist: res.data.data.client,
        })
      }
    })
  },
  //联动项目名称方法
  getXmmc: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxSearchitem', //接口地址
      data: {
        clientId: codes,
      },
      success: function (res) {
        res.data.data.unshift({projectName:'请选择'});
        console.log("数据字典=", res)
        that.setData({
          array4: res.data.data
        })
      }
    })
  },
  // 提交
  formTj: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxaddReimbursementDo', //接口地址
      data:{
        loginName: app.globalData.loginName,
        reimburseUnit: that.data.bmId,
        approvalRoleCodeRemark: that.data.shrId,
        reimburseDetailList:that.data.regionpost,
      }, 
      success: function (res) {
        console.log("数据字典=", res);
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success:function(e){
            setTimeout(function(){
				wx.navigateTo({
				  url: '../app-ywb-home/app-ywb-home',  //提交成功去到你的那个列表页
				})
			},2000)
          }
        })
      }
    })
  },

})